package BandB.Tool.GPhoneManager.Widget.SilentMode;

import BandB.Tool.GPhoneManager.R;
import BandB.Tool.GPhoneManager.Widget.GPhoneWidget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;

public class ToogleWidget extends AppWidgetProvider {
	private final String TAG = ToogleWidget.class.getSimpleName();
	private final int RES_ID_TURN_ON = R.drawable.sound;
	private final int RES_ID_TURN_OFF = R.drawable.sound3;
	private AudioManager audioManager;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		initAirplaneModeSwitcher(context);
	}

	private void initAirplaneModeSwitcher(Context context) {

		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.silent_mode_widget);
		Intent intent = new Intent();
		intent.setAction(GPhoneWidget.ACTION_SILENT_CHANGED);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intent, 0);
		views.setOnClickPendingIntent(R.id.airplane_mode_switch_button,
				pendingIntent);
		int mode = audioManager.getRingerMode();
		if (mode == AudioManager.RINGER_MODE_SILENT) {
			updateAirplaneModeSwitcher(context, RES_ID_TURN_ON);
		} else {
			updateAirplaneModeSwitcher(context, RES_ID_TURN_OFF);
		}

	}

	private void updateAirplaneModeSwitcher(Context context, int resId) {
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.silent_mode_widget);
		Intent intent = new Intent();
		intent.setAction(GPhoneWidget.ACTION_SILENT_CHANGED);
		intent.putExtra("sender", "toogle_widget");
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.airplane_mode_switch_button,
				pendingIntent);
		AppWidgetManager mAppWidgetManager = AppWidgetManager
				.getInstance(context);
		views.setImageViewResource(R.id.airplane_mode_status, resId);
		mAppWidgetManager.updateAppWidget(new ComponentName(context,
				ToogleWidget.class), views);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.i(TAG, "action=" + action);
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.silent_mode_widget);
		if (action.equals(GPhoneWidget.ACTION_SILENT_CHANGED)) {
			if (intent.getStringExtra("sender").equals("toogle_widget")) {
				processSilentModeEvents(context, views, false);
			}else if (intent.getStringExtra("sender").equals("toggles")) {
				processSilentModeEvents(context, views, false);
			} else {
				processSilentModeEvents(context, views, true);
			}
		}

		super.onReceive(context, intent);
	}

	/**
	 * To control the silent mode, we use {@link AudioManager}
	 * 
	 * @param c
	 *            a {@link Context} object
	 * @param views
	 *            a {@link RemoteViews} object
	 */
	private void processSilentModeEvents(Context c, RemoteViews views,
			boolean changeSystem) {

		AudioManager audioManager = (AudioManager) c
				.getSystemService(Context.AUDIO_SERVICE);
		int mode = audioManager.getRingerMode();
		if (mode == AudioManager.RINGER_MODE_SILENT) {
			Log.i(TAG, "current audio mode is silent, turning on...");
			if (changeSystem) {
				audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			}
			views.setImageViewResource(R.id.airplane_mode_status,
					R.drawable.sound);
		} else {
			Log.i(TAG, "current audio mode is non-silent, turning off...");
			if (changeSystem) {
				audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			}
			views.setImageViewResource(R.id.airplane_mode_status,
					R.drawable.sound3);
		}

		AppWidgetManager m = AppWidgetManager.getInstance(c);
		m.updateAppWidget(new ComponentName(c, ToogleWidget.class), views);
	}

}
